package com.erdos.common.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erdos.common.VO.UpcUserVO;
import com.erdos.common.exception.NotLoginException;
import com.erdos.common.util.AppContext;


/**
 * ---------------------------------------------------------------------------------
 * Confidential and Proprietary Copyright 2008 By SGAI & Hewlett-Packard
 * Development Company, L.P. All Rights Reserved
 * 
 * Project Name : SGAI MES Class Name : LoginUtil.java Package :
 * com.hp.common.web $Id: LoginUtil.java 9124 2012-03-08 03:00:56Z hubin $ :
 * 
 * @author xiali2
 * @since 2008-6-6
 * 
 */
public class LoginUtil {

    private static final Log logger = LogFactory.getLog(LoginUtil.class);

    private static final ThreadLocal<UpcUserVO> threadUser = new ThreadLocal<UpcUserVO>();

    private static final String DEFAULT_LOGIN_PIN = "NA";

    private static final String DEFAULT_LOGIN_GROUP = "NA";

    private static final String DEFAULT_LOGIN_SEQ = "NA";

    public static void setUpcUserPin(String pin) {
	if (pin == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + pin + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setUpcUserPin(pin);

	    threadUser.set(upcUserVO);
	}
    }

    /**
     * 设置设备号
     * 
     * @param facilityNo
     */
    public static void setFacilityNo(String facilityNo) {
	if (facilityNo == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + facilityNo + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setFacilityNo(facilityNo);
	    threadUser.set(upcUserVO);
	}
    }

    /**
     * 设置班次
     * 
     * @param shift
     */
    public static void setShift(String shift) {
	if (shift == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + shift + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setShift(shift);
	    threadUser.set(upcUserVO);
	}
    }

    public static void setUpcUserGroup(String userGroup) {
	if (userGroup == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user group[" + userGroup + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setUpcUserGroup(userGroup);

	    threadUser.set(upcUserVO);
	}
    }

    public static void setUpcUserSeq(String userSeq) {
	if (userSeq == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user userSeq[" + userSeq + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setUpcUserSeq(userSeq);

	    threadUser.set(upcUserVO);
	}
    }

    public static void setUpcUserVO(String pin, String userGroup, String authUserSeq, String facilityIds) {
	if (pin == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + pin + "] to threadlocal");
	    UpcUserVO upcUserVO = new UpcUserVO();
	    upcUserVO.setUpcUserPin(pin);

	    if (null != userGroup)
		upcUserVO.setUpcUserGroup(userGroup);

	    if (null != authUserSeq)
		upcUserVO.setUpcUserSeq(authUserSeq);

	    if (null != facilityIds) {
		upcUserVO.setFacilityIds(facilityIds);

	    }

	    threadUser.set(upcUserVO);
	}
    }

    public static void setUpcUserVO(String pin, String userGroup, String shift, String teamTime, String facilityIds,
	    String facilityNo) {
	if (pin == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + pin + "] to threadlocal");
	    UpcUserVO upcUserVO = new UpcUserVO();
	    upcUserVO.setUpcUserPin(pin);

	    if (null != userGroup)
		upcUserVO.setUpcUserGroup(userGroup);

	    if (null != shift)
		upcUserVO.setShift(shift);
	    if (null != teamTime)
		upcUserVO.setTeamTime(teamTime);
	    if (null != facilityIds) {
		upcUserVO.setFacilityIds(facilityIds);

	    }
	    if (null != facilityNo) {
		upcUserVO.setFacilityNo(facilityNo);

	    }
	    threadUser.set(upcUserVO);
	}
    }

    public static void setUpcUserVO(String pin, String upcUserName, String userGroup, String shift, String teamTime,
	    String facilityIds, String facilityNo) {
	if (pin == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + pin + "] to threadlocal");
	    UpcUserVO upcUserVO = new UpcUserVO();
	    upcUserVO.setUpcUserPin(pin);
	    if (null != upcUserName)
		upcUserVO.setUpcUserName(upcUserName);
	    if (null != userGroup)
		upcUserVO.setUpcUserGroup(userGroup);

	    if (null != shift)
		upcUserVO.setShift(shift);
	    if (null != teamTime)
		upcUserVO.setTeamTime(teamTime);
	    if (null != facilityIds) {
		upcUserVO.setFacilityIds(facilityIds);

	    }
	    if (null != facilityNo) {
		upcUserVO.setFacilityNo(facilityNo);

	    }
	    threadUser.set(upcUserVO);
	}
    }

    public static void setUpcUserVO(String pin, String userGroup) {
	setUpcUserVO(pin, userGroup, null, null);
    }

    public static String getUpcUserGroup() {
	UpcUserVO upcUserVO = threadUser.get();

	String userGroup = null;
	if (upcUserVO != null) {
	    userGroup = upcUserVO.getUpcUserGroup();
	}
	logger.debug("user group get from threadLocal=" + userGroup);
	if (userGroup == null)
	    userGroup = DEFAULT_LOGIN_PIN;
	return userGroup;
    }

    public static String getUpcUserPin() {
	UpcUserVO upcUserVO = threadUser.get();
	String pin = null;
	if (upcUserVO != null) {
	    pin = upcUserVO.getUpcUserPin();
	}

	// logger.debug("user pin get from threadLocal="+pin);
	if (pin == null)
	    pin = DEFAULT_LOGIN_GROUP;
	return pin;
    }

    public static String getUpcUserSeq() {
	UpcUserVO upcUserVO = threadUser.get();
	String seq = null;
	if (upcUserVO != null) {
	    seq = upcUserVO.getUpcUserSeq();
	}

	// logger.debug("user pin get from threadLocal="+pin);
	if (seq == null)
	    seq = DEFAULT_LOGIN_SEQ;
	return seq;
    }

    public static void checkIfHasValidLoginInfo() {
	String pin = getUpcUserPin();
	if (pin == null || pin.equals(DEFAULT_LOGIN_PIN)) {
	    logger.warn("Not login access!");
	    throw new NotLoginException();
	}
    }

    public static boolean isHaveValidLoginInfo() {
	String pin = getUpcUserPin();
	return !(pin == null || pin.equals(DEFAULT_LOGIN_PIN));
    }

    public static String getFacilityIds() {
	UpcUserVO upcUserVO = threadUser.get();
	String facilityIds = null;
	if (upcUserVO != null) {
	    facilityIds = upcUserVO.getFacilityIds();
	}

	// logger.debug("user pin get from threadLocal="+pin);
	if (facilityIds == null)
	    facilityIds = AppContext.getFacility();
	return facilityIds;
    }

    public static void setFacilityIds(String facilityIds) {
	if (facilityIds == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user facilityIds[" + facilityIds + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setFacilityIds(facilityIds);

	    threadUser.set(upcUserVO);
	}
    }

    /**
     * 获取设备编号
     * 
     * @return
     * @author issuser
     * @since 2009-10-8
     */
    public static String getFacilityNo() {
	String facilityNo = "NA";
	UpcUserVO upcUserVO = threadUser.get();
	if (upcUserVO != null) {
	    facilityNo = upcUserVO.getFacilityNo();
	}
	return facilityNo;
    }

    /**
     * 获取班次
     */
    public static String getShift() {
	String shift = "NA";
	UpcUserVO upcUserVO = threadUser.get();
	if (upcUserVO != null) {
	    shift = upcUserVO.getShift();
	}
	return shift;
    }

    /**
     * 设置班时
     * 
     * @param teamTime
     * @author issuser
     * @since 2009-11-26
     */
    public static void setTeamTime(String teamTime) {
	if (teamTime == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + teamTime + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setTeamTime(teamTime);
	    threadUser.set(upcUserVO);
	}

    }

    /**
     * 获取班次
     */
    public static String getTeamTime() {
	String teamTime = "NA";
	UpcUserVO upcUserVO = threadUser.get();
	if (upcUserVO != null) {
	    teamTime = upcUserVO.getTeamTime();
	}
	return teamTime;
    }

    /**
     * 设置用户名;
     * 
     * @param name
     * @author issuser
     * @since 2009-12-3
     */
    public static void setUpcUserName(String name) {
	if (name == null) {
	    logger.debug("clean up threadlocal");
	    threadUser.remove();
	} else {
	    logger.debug("put user[" + name + "] to threadlocal");

	    UpcUserVO upcUserVO = threadUser.get();

	    if (upcUserVO == null) {
		upcUserVO = new UpcUserVO();
	    }
	    upcUserVO.setUpcUserName(name);

	    threadUser.set(upcUserVO);
	}
    }

    public static String getUpcUserName() {
	String upcUserName = "NA";
	UpcUserVO upcUserVO = threadUser.get();
	if (upcUserVO != null) {
	    upcUserName = upcUserVO.getUpcUserName();
	}else{
		upcUserName =  LoginUtil.getUpcUserPin();
	}
	return upcUserName;
    }
}